<?php
class account extends Zend_Db_Table
{

	protected $_schema='softlab';
	protected $_name='account';
	protected $_primary='AId';
	protected $_sequence=true;

	// account db_helper
	protected $dbh;

	public function __construct()
	{
		$config=array('db'=>'database');

		include 'db_helper.php';
		$this->dbh=new db_helper(null, 'account',
					'AId(pk)|int'.
					'||User|string(4, 20)'.
					'||Pass|string'.
					'||Priv|int'.
					'||Sort|int'.
					'||Status|int|fmt_status'.
					'||Name|string(4, 20)'.
					'||Icon(null)|string'.
					'||Race(null)|string'.
					'||Birth(null)|time(1930-1-1, 2000-1-1)'.
						'|_time(%Y年生)|_time(%Y)'.
					'||Sex(null)|string(0, 1)|fmt_sex'.
					'||Education(null)|string'.
					'||Title(null)|string'.
					'||Intro(null)|string|_html|_textarea'.
					'||Interest(null)|string|_html|_textarea'.
					'||MSN(null)|string'.
					'||Email(null)|string|_a'.
					'||Tel(null)|string'.
					'||Home(null)|string|_a'.
					'||Blog(null)|string|_a'.
					'||QQ(null)|string'.
					'||Job(null)|string'.
					'||EnrollDate|time|_time(%Y年9月)|_time(%Y)');
		
		parent::__construct($config);
	}

	// 格式化身份(status)
	static function fmt_status($v)
	{
		switch ($v) {
		case 1: $v='在读硕士'; break;
		case 2: $v='在读博士'; break;
		case 3: $v='教师'; break;
		case 4: $v='讲师'; break;
		case 5: $v='副教授'; break;
		case 6: $v='教授'; break;
		case 7: $v='教授，博士生导师'; break;
		default: $v='毕业生';
		}
		return $v;
	}

	// 格式化性别(sex)
	static function fmt_sex($v)
	{
		if ($v=='m') 
			return '男';
		else if ($v=='f')
			return '女';
		return '';
	}

	function insert(array $data)
	{
		return parent::insert($this->dbh->filter($data));
	}

	// 过滤掉不再表中的参数
	public function filter($data)
	{
		return $this->dbh->filter($data);
	}

	// 检测参数是否符合规范
	public function check($data, $ignore=false)
	{
		return $this->dbh->check($data, $ignore);
	}

	// 出错字段
	public function err_field()
	{
		return $this->dbh->field;
	}

	// 出错信息
	public function err_msg()
	{
		return $this->dbh->error;
	}

	// 取出成员信息
	public function member($aid)
	{
		try {
			$rowset=parent::find($aid);
			$row=$rowset->current();
			return $row->toArray();
		}
		catch (Zend_Exception $e) {
			return null;
		}
	}

	// 格式化一个人的信息
	public function format($data, $edit=false)
	{
		if ($edit) $edit='<i style="color:#999">编辑</i>';
		return $this->dbh->format($data, array(), $edit, $this);
	}

	// 取得所有成员，分别存入$teacher、$phd、$master、$graduate里。
	public function all_member(&$teacher, &$phd, &$master, &$graduate)
	{
		$result=parent::_fetch(null, array('Sort desc'));
		foreach ($result as $row) {
			switch ($row['Status']) {
			case 7:
			case 6:
			case 5:
			case 4:
			case 3:
				array_push($teacher, self::format($row, false));
				break;
			case 2:
				array_push($phd, self::format($row, false));
				break;
			case 1:
				array_push($master, self::format($row, false));
				break;
			default:
				array_push($graduate, self::format($row, false));
			} // end of switch
		} // end of foreach
	}

	// 取得教室成员。
	public function teacher(&$t)
	{
		$result=parent::_fetch('Status>=3', array('Sort desc'));
		foreach ($result as $row) {
			array_push($t, self::format($row, false));
		}
	}

	// 更新成员信息
	public function update_member($data, $aid)
	{
		try {
			$data=$this->dbh->filter($data);

			if (isset($data['AId'])) unset($data['AId']);
			return parent::update($data, 'AId='.intval($aid));
		}
		catch (Exception $e) {
			return -1;
		}
	}

	// 更新密码, 成功返回1，0旧密码错
	public function change_pass($aid, $old, $new)
	{
		$rows = parent::find($aid);
		$row = $rows->current();
		$d = $row->toArray();
		if ($d['Pass']!=$old)
			return 0;
		$row->Pass = $new;
		$row->save();
		return 1;
	}

};
?>
